package com.guguskins.back.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.guguskins.back.common.websocket.PayWebSocketServer;
import com.guguskins.back.entity.GuBox;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.service.GuBoxService;
import com.guguskins.back.service.GuSystemConfigService;
import com.guguskins.back.utils.JwtUtil;
import com.guguskins.back.utils.SendMailUtil;
import com.guguskins.back.utils.steam.SteamLoginUtil;
import com.guguskins.back.utils.steam.SteamUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Map;

/**
 * @author duttonliu
 * @date 2021/4/9 10:58
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private GuBoxService boxService;

    @Autowired
    private GuSystemConfigService systemConfigService;

    @RequestMapping("/test")
    public void test(@RequestParam String id,
                     @RequestParam boolean flag) throws IOException {
        PayWebSocketServer.sendInfo(JSON.toJSONString(flag ? ResultVo.success("充值成功") : ResultVo.fail("充值失败")), id);
    }

    @GetMapping("/send")
    public void send() {
        SendMailUtil.sendSimpleMail("873402131@qq.com", null, "测试", "测试内容");
    }

    @GetMapping("/steamCallback")
    public void steamCallBackGet(@RequestParam(required = false) Map<String, String> map) {
        System.err.println("GET");
        System.err.println(map);
        System.err.println(SteamLoginUtil.validate(map));
    }

    @GetMapping("/getOwnedGames")
    public JSONObject getOwnedGames(String steamId) {
        JSONObject ownedGames = SteamUtil.getOwnedGames(steamId, systemConfigService.getSystemConfig().getConfigSteamKey());
        System.err.println(ownedGames);
        return ownedGames;
    }

    @GetMapping("/getPlayerBans")
    public String getPlayerBans(String steamIds) {
        String playerBans = SteamUtil.getPlayerBans(steamIds, systemConfigService.getSystemConfig().getConfigSteamKey());
        System.err.println(playerBans);
        return playerBans;

    }

    @GetMapping("/getInventory")
    public String getInventory(String steamId) {
        String inventory = SteamUtil.getInventory(steamId).toString();
        System.err.println(inventory);
        return inventory;
    }

    @GetMapping("result")
    public ResultVo result() {
        return ResultVo.success(new Object());
    }

    @GetMapping("getToken")
    public ResultVo getToken() {
        String token = JwtUtil.generateToken(2L);
        return ResultVo.success(token);
    }

    @GetMapping("test")
    public ResultVo test() {
        return ResultVo.success("test");
    }

    @GetMapping(value = "/json", produces="application/json;charset=UTF-8")
    public ResultVo get() {
        GuBox byId = boxService.getById(36);
        System.err.println(byId);
        return ResultVo.success(byId);
    }

    @GetMapping("/steamCallbackTest")
    //重定向
    public ModelAndView steamCallbackTest() {

        String token = JwtUtil.generateToken(2L);
//        return ResultVo.success(user);
//        return "redirect:http://192.168.0.119:8000/#/home?key=" + CommonUtil.base64Encoder(token);
        return new ModelAndView("redirect:http://192.108.0.119:8000/#/home?token=" + token);
//        response.sendRedirect("http://192.168.0.119:8000/#/home");
    }

    @GetMapping("/testN1")
    public void get1(@Param("A") int a) {
        System.err.println(a);
    }

    @GetMapping("/testN2")
    public void get2(@RequestParam("A") int a) {
        System.err.println(a);
    }

    @PostMapping("/json")
    public void json(@RequestParam Map<String, Object> map) {
        System.err.println(JSON.toJSONString(map));
    }
}
